package com.wu.serviceImpl.sys;

import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.wu.base.ApiResponse;
import com.wu.base.ResultUtils;
import com.wu.constants.Constants;
import com.wu.entity.sys.*;
import com.wu.enums.LoginEnum;
import com.wu.enums.SystemEnum;
import com.wu.enums.UserSessionEnum;
import com.wu.enums.field.FlagEnum;
import com.wu.enums.field.UserTypeEnum;
import com.wu.exception.BusinessException;
import com.wu.exception.ProjectExceptionEnum;
import com.wu.mapper.*;
import com.wu.pojo.dto.SessionUserDto;
import com.wu.pojo.reqeust.CommonIdRequest;
import com.wu.pojo.reqeust.CommonIdStatusRequest;
import com.wu.pojo.reqeust.sys.*;
import com.wu.pojo.response.CommonIdNameResponse;
import com.wu.pojo.response.sys.LoginResponse;
import com.wu.pojo.response.sys.UserListResponse;
import com.wu.pojo.response.sys.UserOptionListResponse;
import com.wu.redis.RedisService;
import com.wu.service.sys.SUserService;
import com.wu.util.DozerUtil;
import com.wu.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 用户表(SUser)表服务实现类
 *
 * @since 2020-12-29 13:20:30
 */
@Slf4j
@Service("sUserService")
public class SUserServiceImpl extends ServiceImpl<SUserMapper, SUserDo> implements SUserService {
    @Resource
    private SUserMapper sUserMapper;
    @Resource
    private SUserRoleMapper userRoleMapper;
    @Resource
    private SRoleResourceMapper roleResourceMapper;
    @Resource
    private SResourceMapper resourceMapper;
    @Resource
    private HttpServletRequest httpServletRequest;
    @Resource
    private SUserPositionMapper userPositionMapper;
    @Resource
    private SPositionMapper positionMapper;
    @Resource
    private SDepartmentMapper departmentMapper;
    @Resource
    private SRoleMapper roleMapper;
    @Resource
    private RedisService redisService;

    @Value("${spring.profiles.active}")
    private String env;
    @Value("${default.password}")
    private String defaultPassword;


    @Override
    public ApiResponse login(LoginRequest loginRequest) {
        // 验证验证码  测试环境未校验
        if (!Constants.DEV.equals(env) && !numCode(loginRequest.getCodeNum())) {
            return ResultUtils.setError(LoginEnum.VERY_NUM_CODE_FAIL);
        }
        return ResultUtils.setOk(loginProcess(loginRequest.getUsername(), loginRequest.getPassword()));
    }

    @Override
    public ApiResponse phoneLogin(PhoneLoginRequest request) {
        //todo  验证码校验
        QueryWrapper<SUserDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("phone", request.getPhone())
                .last("Limit 1");
        SUserDo sUserDo = sUserMapper.selectOne(queryWrapper);
        if (null == sUserDo) {
            return ResultUtils.setError(LoginEnum.USER_LOGIN_FAIL);
        }
        return ResultUtils.setOk(loginProcess(sUserDo.getUsername(), sUserDo.getPassword()));
    }

    @Override
    public SessionUserDto getUserDoByUsername(String userName) {
        return sUserMapper.getSUserDoByUsername(userName);
    }

    @Override
    public ApiResponse list(UserListRequest request) {

        List<Long> departmentSubIds = new ArrayList<>();
        if (null != request.getDepartmentId()) {
            SDepartmentDo departmentDo = departmentMapper.selectById(request.getDepartmentId());
            if (null == departmentDo || FlagEnum.DELETE.getCode().equals(departmentDo.getFlag())) {
                return ResultUtils.setError(SystemEnum.DEPARTMENT_NOT_EXIST);
            }
            if (departmentDo.getParentId() != 0) {
                //改为只查当前所查部门
//                departmentSubIds = subIds(Lists.newArrayList(request.getDepartmentId()), Lists.newArrayList());
                departmentSubIds.add(request.getDepartmentId());
            }
        }

        PageHelper.startPage(request.getCurrentPage(), request.getPageSize());
        List<UserListResponse> userListResponses = sUserMapper.userList(request.getName(), departmentSubIds);
        for (UserListResponse userListResponse : userListResponses) {
            setUserInfo(userListResponse);
        }
        return ResultUtils.setOk(new PageInfo<>(userListResponses));
    }

    @Override
    public ApiResponse optionList(UserListRequest request) {
        List<Long> departmentSubIds = null;
        if (null != request.getDepartmentId()) {
            SDepartmentDo departmentDo = departmentMapper.selectById(request.getDepartmentId());
            if (null == departmentDo || FlagEnum.DELETE.getCode().equals(departmentDo.getFlag())) {
                return ResultUtils.setError(SystemEnum.DEPARTMENT_NOT_EXIST);
            }
            if (departmentDo.getParentId() != 0) {
                departmentSubIds = subIds(Lists.newArrayList(request.getDepartmentId()), Lists.newArrayList());
                departmentSubIds.add(request.getDepartmentId());
            }
        }
        return ResultUtils.setOk(sUserMapper.userOptionList(request.getName(), departmentSubIds));
    }

    @Transactional
    @Override
    public ApiResponse save(UserSaveRequest request) {

        QueryWrapper<SUserDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", request.getPhone());
        queryWrapper.eq("flag", FlagEnum.NORMAL.getCode());
        Integer selectCount = sUserMapper.selectCount(queryWrapper);
        if (selectCount > 0) {
            return ResultUtils.setError("手机号已存在");
        }
        if (StringUtils.isNotBlank(request.getIdCard())) {
            boolean validCard18 = IdcardUtil.isValidCard18(request.getIdCard());
            if (!validCard18) {
                return ResultUtils.setError("身份证号非法");
            }
        }
        //部门
        QueryWrapper<SDepartmentDo> queryDepartment = new QueryWrapper<>();
        queryDepartment.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("id", request.getDepartmentId());
        SDepartmentDo sDepartmentDo = departmentMapper.selectOne(queryDepartment);
        if (null == sDepartmentDo) {
            throw new BusinessException(SystemEnum.DEPARTMENT_NOT_EXIST);
        }

        Long currentUserId = request.getCurrentUserId();
        SUserDo map = DozerUtil.map(request, SUserDo.class);
        String sl = map.getPhone().substring(5);
        map.setUsername(map.getPhone());
        map.setPassword(MD5Util.passwordMD5(defaultPassword, sl));
        map.setSalt(sl);
        map.setCreater(currentUserId);
        int insert = sUserMapper.insert(map);
        if (insert < 1) {
            return ResultUtils.setError(SystemEnum.FAILED);
        }
        Long userId = map.getId();
        //职位
        setUserPosition(request.getPositionIds(), currentUserId, userId);
        //角色
        setUserRole(request.getRoleIds(), currentUserId, userId);

        return ResultUtils.setOk();
    }

    @Transactional
    @Override
    public ApiResponse edit(UserEditRequest request) {
        SUserDo sUserDo = sUserMapper.selectById(request.getId());
        if (null == sUserDo || FlagEnum.DELETE.getCode().equals(sUserDo.getFlag())) {
            return ResultUtils.setError(SystemEnum.USER_NOT_EXIST);
        }
        if (request.getId().equals(request.getSuperiorId())) {
            return ResultUtils.setError("直属上级不可以为自己");
        }
        QueryWrapper<SUserDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", request.getPhone())
                .ne("id", sUserDo.getId())
                .eq("flag", FlagEnum.NORMAL.getCode());
        Integer selectCount = sUserMapper.selectCount(queryWrapper);
        if (selectCount > 0) {
            return ResultUtils.setError("手机号已存在");
        }
        if (StringUtils.isNotBlank(request.getIdCard())) {
            boolean validCard18 = IdcardUtil.isValidCard18(request.getIdCard());
            if (!validCard18) {
                return ResultUtils.setError("身份证号非法");
            }
        }
        //部门
        QueryWrapper<SDepartmentDo> queryDepartment = new QueryWrapper<>();
        queryDepartment.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("id", request.getDepartmentId());
        SDepartmentDo sDepartmentDo = departmentMapper.selectOne(queryDepartment);
        if (null == sDepartmentDo) {
            throw new BusinessException(SystemEnum.DEPARTMENT_NOT_EXIST);
        }
        Long userId = sUserDo.getId();
        Long currentUserId = request.getCurrentUserId();
        SUserDo map = DozerUtil.map(request, SUserDo.class);
        map.setUsername(map.getPhone());
        map.setModifier(currentUserId);
        map.setModifyTime(new Date());
        int updateById = sUserMapper.updateById(map);
        if (updateById < 1) {
            return ResultUtils.setError(SystemEnum.FAILED);
        }

        //职位
        setUserPosition(request.getPositionIds(), currentUserId, userId);
        //角色
        int i = setUserRole(request.getRoleIds(), currentUserId, userId);
        if (i > 0) {
            redisService.del(Constants.USER_RESOURCES + request.getId(), Constants.SHIRO_CACHE + Constants.SHIRO_USER_RESOURCES + ":" + request.getId());
        }
        return ResultUtils.setOk();
    }

    @Transactional
    @Override
    public ApiResponse delete(CommonIdRequest request) {
        Long userId = request.getId();
        SUserDo sUserDo = sUserMapper.selectById(userId);
        if (null == sUserDo || FlagEnum.DELETE.getCode().equals(sUserDo.getFlag())) {
            return ResultUtils.setError(SystemEnum.USER_NOT_EXIST);
        }
        if (UserTypeEnum.ADMIN.getCode().equals(sUserDo.getType())) {
            return ResultUtils.setError("不可删除管理员");
        }
        if (request.getCurrentUserId().equals(request.getId())) {
            return ResultUtils.setError("不可删除自己");
        }

        QueryWrapper<SUserDo> querySuperior = new QueryWrapper<>();
        querySuperior.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("superior_id", request.getId());
        List<SUserDo> sUserDos = sUserMapper.selectList(querySuperior);
        if (CollectionUtils.isNotEmpty(sUserDos)) {
            List<String> collect = sUserDos.stream().map(SUserDo::getRealName).collect(toList());
            return ResultUtils.setError(StringUtils.join(collect, ",") + " 的直属上级为此人员，请先更换后再删除");
        }

        delUserRole(userId, request.getCurrentUserId());
        delUserPosition(userId, request.getCurrentUserId());

        sUserDo.setModifier(request.getCurrentUserId());
        sUserDo.setFlag(FlagEnum.DELETE.getCode());
        sUserDo.setModifyTime(new Date());
        int updateById = sUserMapper.updateById(sUserDo);
        if (updateById < 1) {
            return ResultUtils.setError(SystemEnum.FAILED);
        }
        return ResultUtils.setOk();
    }

    @Override
    public ApiResponse onOrOff(CommonIdStatusRequest request) {
        return null;
    }

    @Override
    public ApiResponse getById(CommonIdRequest request) {
        SUserDo sUserDo = sUserMapper.selectById(request.getId());
        if (null == sUserDo || FlagEnum.DELETE.getCode().equals(sUserDo.getFlag())) {
            return ResultUtils.setError(SystemEnum.USER_NOT_EXIST);
        }
        UserListResponse map = DozerUtil.map(sUserDo, UserListResponse.class);
        setUserInfo(map);
        return ResultUtils.setOk(map);
    }

    @Override
    public ApiResponse defaultPwd(CommonIdRequest request) {
        SUserDo userDo = sUserMapper.selectById(request.getCurrentUserId());
        if (null == userDo || FlagEnum.DELETE.getCode().equals(userDo.getFlag())) {
            return ResultUtils.setError(SystemEnum.USER_NOT_EXIST);
        }
        SUserDo sUserDo = sUserMapper.selectById(request.getId());
        if (null == sUserDo || FlagEnum.DELETE.getCode().equals(sUserDo.getFlag())) {
            return ResultUtils.setError(SystemEnum.USER_NOT_EXIST);
        }
        sUserDo.setModifier(request.getCurrentUserId());
        sUserDo.setPassword(MD5Util.passwordMD5(defaultPassword, userDo.getSalt()));
        sUserDo.setModifyTime(new Date());
        sUserMapper.updateById(sUserDo);
        return ResultUtils.setOk();
    }

    @Override
    public ApiResponse editPwd(EditPasswordRequest request) {
        SUserDo userDo = sUserMapper.selectById(request.getCurrentUserId());
        if (null == userDo || FlagEnum.DELETE.getCode().equals(userDo.getFlag())) {
            return ResultUtils.setError(SystemEnum.USER_NOT_EXIST);
        }
        String forPass = MD5Util.passwordMD5(request.getFormerPassword(), userDo.getSalt());
        if (!forPass.equals(userDo.getPassword())) {
            return ResultUtils.setError("原密码错误 请重试");
        }
        userDo.setModifier(request.getCurrentUserId());
        userDo.setPassword(MD5Util.passwordMD5(request.getPassword(), userDo.getSalt()));
        userDo.setModifyTime(new Date());
        sUserMapper.updateById(userDo);
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return ResultUtils.setOk();
    }

    @Override
    public List<UserOptionListResponse> getUsersByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        return sUserMapper.getUsersByIds(ids);
    }

    @Override
    public List<UserOptionListResponse> getUsersByDepartmentId(Long id) {
        if (null == id || 0 == id) {
            return Lists.newArrayList();
        }
        List<Long> longs = subIds(Lists.newArrayList(id), null);
        longs.add(id);
        return sUserMapper.getDepartmentUsersByIds(longs);
    }

    private List<Long> subIds(List<Long> ids, List<Long> resultIds) {
        if (CollectionUtils.isEmpty(resultIds)) {
            resultIds = new ArrayList<>();
        }
        QueryWrapper<SDepartmentDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                .in("parent_id", ids);
        List<SDepartmentDo> sDepartmentDos = departmentMapper.selectList(queryWrapper);
        List<Long> current = sDepartmentDos.stream().map(SDepartmentDo::getId).collect(toList());
        if (CollectionUtils.isEmpty(current)) {
            return resultIds;
        }
        resultIds.addAll(current);
        return subIds(current, resultIds);
    }


    private LoginResponse loginProcess(String username, String password) {
        //host 用来判断前后台登陆。
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        SessionUserDto sUserDo;
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            sUserDo = (SessionUserDto) subject.getSession().getAttribute(UserSessionEnum.SESSION_USER_KEY.getKey());
        } catch (AuthenticationException e) {
            token.clear();
            throw new BusinessException(ProjectExceptionEnum.USER_LOGIN_FAIL);
        } catch (Exception e2) {
            token.clear();
            throw new BusinessException(ProjectExceptionEnum.SYSTEM_ERROR);
        }
        LoginResponse loginResponse = DozerUtil.map(sUserDo, LoginResponse.class);
        SUserDo userDo = sUserMapper.selectById(loginResponse.getId());
        loginResponse.setName(userDo.getUsername());
        loginResponse.setId(userDo.getId());
        loginResponse.setPortrait(userDo.getPortrait());

        List<SResourceDo> sResourceByUserId = resourceMapper.getSResourceByUserId(sUserDo.getId(), 0);
        Set<String> collect = sResourceByUserId.stream().map(SResourceDo::getCode).collect(Collectors.toSet());
        loginResponse.setResources(collect);
        return loginResponse;
    }

    /**
     * 数字验证码
     *
     * @param code
     * @return
     */
    private boolean numCode(String code) {
        HttpSession session = httpServletRequest.getSession();
        if (null == session) {
            return false;
        }
        Object codeNum = session.getAttribute("codeNum");
        if (Objects.isNull(codeNum) || StringUtils.isBlank(code)) {
            return false;
        }
        if (!code.equalsIgnoreCase(codeNum.toString())) {
            return false;
        }
        return true;
    }

    private void setUserPosition(List<Long> positionIds, Long currentUserId, Long userId) {
        List<Long> updateIds = new ArrayList<>();
        List<Long> insertIds = new ArrayList<>(positionIds);

        if (CollectionUtils.isEmpty(positionIds)) {
            UpdateWrapper<SUserPositionDo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                    .eq("user_id", userId);
            SUserPositionDo sUserPositionDo = new SUserPositionDo();
            sUserPositionDo.setModifier(currentUserId);
            sUserPositionDo.setFlag(FlagEnum.DELETE.getCode());
            sUserPositionDo.setModifyTime(new Date());
            int update = userPositionMapper.update(sUserPositionDo, updateWrapper);
            if (update < 1) {
                throw new BusinessException(SystemEnum.FAILED);
            }
            return;
        } else {
            QueryWrapper<SUserPositionDo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                    .eq("user_id", userId);
            List<SUserPositionDo> sUserPositionDos = userPositionMapper.selectList(queryWrapper);
            List<Long> collect = sUserPositionDos.stream().map(SUserPositionDo::getPositionId).collect(Collectors.toList());
            updateIds.addAll(collect);
            updateIds.removeAll(insertIds);
            insertIds.removeAll(collect);
        }
        QueryWrapper<SPositionDo> queryPosition = new QueryWrapper<>();
        queryPosition.eq("flag", FlagEnum.NORMAL.getCode())
                .in("id", positionIds);
        Integer selectCount = positionMapper.selectCount(queryPosition);
        if (selectCount != positionIds.size()) {
            throw new BusinessException(SystemEnum.POSITION_NOT_EXIST);
        }

        updateIds.removeAll(Collections.singleton(null));
        insertIds.removeAll(Collections.singleton(null));
        if (CollectionUtils.isNotEmpty(updateIds)) {
            UpdateWrapper<SUserPositionDo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                    .in("position_id", updateIds)
                    .eq("user_id", userId);
            SUserPositionDo sUserPositionDo = new SUserPositionDo();
            sUserPositionDo.setModifier(currentUserId);
            sUserPositionDo.setFlag(FlagEnum.DELETE.getCode());
            sUserPositionDo.setModifyTime(new Date());
            int update = userPositionMapper.update(sUserPositionDo, updateWrapper);
            if (update < 1) {
                throw new BusinessException(SystemEnum.FAILED);
            }
        }
        if (CollectionUtils.isNotEmpty(insertIds)) {
            SUserPositionDo userPositionDo = new SUserPositionDo();
            userPositionDo.setUserId(userId);
            userPositionDo.setCreater(currentUserId);
            for (Long positionId : insertIds) {
                userPositionDo.setPositionId(positionId);
                int insert = userPositionMapper.insert(userPositionDo);
                if (insert < 1) {
                    throw new BusinessException(SystemEnum.FAILED);
                }
            }
        }
    }

    private int setUserRole(List<Long> roleIds, Long currentUserId, Long userId) {
        List<Long> updateIds = new ArrayList<>();
        List<Long> insertIds = new ArrayList<>(roleIds);
        int i = 0;
        if (CollectionUtils.isEmpty(roleIds)) {
            UpdateWrapper<SUserRoleDo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                    .eq("user_id", userId);
            SUserRoleDo sUserRoleDo = new SUserRoleDo();
            sUserRoleDo.setModifier(currentUserId);
            sUserRoleDo.setFlag(FlagEnum.DELETE.getCode());
            sUserRoleDo.setModifyTime(new Date());
            int update = userRoleMapper.update(sUserRoleDo, updateWrapper);
            if (update < 1) {
                throw new BusinessException(SystemEnum.FAILED);
            }
            i++;
            return i;
        } else {
            QueryWrapper<SUserRoleDo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                    .eq("user_id", userId);
            List<SUserRoleDo> sUserRoleDos = userRoleMapper.selectList(queryWrapper);
            List<Long> collect = sUserRoleDos.stream().map(SUserRoleDo::getRoleId).collect(Collectors.toList());
            updateIds.addAll(collect);
            updateIds.removeAll(insertIds);
            insertIds.removeAll(collect);
        }

        QueryWrapper<SRoleDo> queryRole = new QueryWrapper<>();
        queryRole.eq("flag", FlagEnum.NORMAL.getCode())
                .in("id", roleIds);
        Integer selectCount = roleMapper.selectCount(queryRole);
        if (selectCount != roleIds.size()) {
            throw new BusinessException(SystemEnum.ROLE_NOT_EXIST);
        }

        updateIds.removeAll(Collections.singleton(null));
        insertIds.removeAll(Collections.singleton(null));
        if (CollectionUtils.isNotEmpty(updateIds)) {
            UpdateWrapper<SUserRoleDo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                    .in("role_id", updateIds)
                    .eq("user_id", userId);
            SUserRoleDo sUserRoleDo = new SUserRoleDo();
            sUserRoleDo.setModifier(currentUserId);
            sUserRoleDo.setFlag(FlagEnum.DELETE.getCode());
            sUserRoleDo.setModifyTime(new Date());
            int update = userRoleMapper.update(sUserRoleDo, updateWrapper);
            if (update < 1) {
                throw new BusinessException(SystemEnum.FAILED);
            }
            i++;
        }
        if (CollectionUtils.isNotEmpty(insertIds)) {
            SUserRoleDo sUserRoleDo = new SUserRoleDo();
            sUserRoleDo.setUserId(userId);
            sUserRoleDo.setCreater(currentUserId);
            for (Long roleId : insertIds) {
                sUserRoleDo.setRoleId(roleId);
                int insert = userRoleMapper.insert(sUserRoleDo);
                if (insert < 1) {
                    throw new BusinessException(SystemEnum.FAILED);
                }
            }
            i++;
        }
        return i;
    }

    private void setUserInfo(UserListResponse userListResponse) {
        if (null == userListResponse) {
            return;
        }
        if (CollectionUtils.isEmpty(userListResponse.getPositionList())) {
            List<SPositionDo> positionsByUserId = positionMapper.getPositionsByUserId(userListResponse.getId());
            List<CommonIdNameResponse> positions = DozerUtil.mapList(positionsByUserId, CommonIdNameResponse.class);
            userListResponse.setPositionList(positions);
        }

        if (StringUtils.isBlank(userListResponse.getDepartmentName())) {
            SDepartmentDo departmentByUserId = departmentMapper.selectById(userListResponse.getDepartmentId());
            if (null != departmentByUserId) {
                userListResponse.setDepartmentId(departmentByUserId.getId());
                userListResponse.setDepartmentName(departmentByUserId.getName());
            }
        }

        if (CollectionUtils.isEmpty(userListResponse.getRoleList())) {
            List<SRoleDo> roleListByUserId = roleMapper.getRoleListByUserId(userListResponse.getId());
            List<CommonIdNameResponse> commonIdNameResponses = DozerUtil.mapList(roleListByUserId, CommonIdNameResponse.class);
            userListResponse.setRoleList(commonIdNameResponses);
        }
    }

    private void delUserRole(Long userId, Long currentUserId) {
        UpdateWrapper<SUserRoleDo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("user_id", userId);
        SUserRoleDo sUserRoleDo = new SUserRoleDo();
        sUserRoleDo.setModifier(currentUserId);
        sUserRoleDo.setFlag(FlagEnum.DELETE.getCode());
        sUserRoleDo.setModifyTime(new Date());
        int update = userRoleMapper.update(sUserRoleDo, updateWrapper);
    }

    private void delUserPosition(Long userId, Long currentUserId) {
        UpdateWrapper<SUserPositionDo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("flag", FlagEnum.NORMAL.getCode())
                .eq("user_id", userId);
        SUserPositionDo sUserPositionDo = new SUserPositionDo();
        sUserPositionDo.setModifier(currentUserId);
        sUserPositionDo.setFlag(FlagEnum.DELETE.getCode());
        sUserPositionDo.setModifyTime(new Date());
        int update = userPositionMapper.update(sUserPositionDo, updateWrapper);
    }

}